﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;

public partial class CommonPages_SelectOperator : BasePage
{
    private static string txtName;
    private static string txtId; 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtName = Request.QueryString["Name"];
            txtId = Request.QueryString["Id"];
            if (Session["FlagId"] == null)
            {
                Session["FlagId"] = txtId;
            }
            else
            {
                if (Session["FlagId"].ToString() != txtId)
                {
                    Session["FlagId"] = txtId;
                    Session["tmpUserTable"] = null;
                }
            }
            BuildTreeRoot();
            CreatTmpTable();
        }
    }

    #region 树相关函数


    //构建部门树根节点
    private void BuildTreeRoot()
    {
        DataTable tmpDt = CommonLib.BLL.SYS_DEPT.GetListSub(" dept_id='00' and  del_flag =0 ", "dept_id").Tables[0];
        if (tmpDt.Rows.Count == 1)
        {
            Ext.Net.AsyncTreeNode root = new Ext.Net.AsyncTreeNode();
            root.Text = tmpDt.Rows[0]["dept_name"].ToString();
            root.NodeID = "A" + tmpDt.Rows[0]["dept_id"].ToString();
            root.Expanded = true;
            root.IconCls = "no-Icon";
            root.Checked = ThreeStateBool.False;
            if (tmpDt.Rows[0]["leaf"].ToString() == "0")
                root.Leaf = true;
            root.Draggable = true;
            this.TreePanel1.Root.Add(root);
        }
    }

    [DirectMethod]
    public string NodeLoad(string nodeID, bool ischeck)
    {
        nodeID = nodeID.Substring(1, nodeID.Length - 1);
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and rtrim(dept_id) like '" + nodeID.Trim() + "__' ", "dept_id").Tables[0];
            for (int i = 0; i < TreeData.Rows.Count; i++)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = TreeData.Rows[i]["dept_name"].ToString();
                asyncNode.NodeID = "A" + TreeData.Rows[i]["dept_id"].ToString();
                asyncNode.IconCls = "no-Icon";
                DataTable dtt = CommonLib.BLL.SYS_USER.GetList(" dept_id='" + TreeData.Rows[i]["dept_id"].ToString() + "'").Tables[0];
                if (TreeData.Rows[i]["leaf"].ToString() == "0" && dtt.Rows.Count == 0)
                    asyncNode.Leaf = true;
                else
                {
                    if (ischeck)
                        asyncNode.Checked = ThreeStateBool.True;
                    else
                        asyncNode.Checked = ThreeStateBool.False;
                }
                nodes.Add(asyncNode);
            }
            DataTable TmpTable = CommonLib.BLL.SYS_USER.GetList(" dept_id='" + nodeID.Trim() + "'").Tables[0];
            for (int j = 0; j < TmpTable.Rows.Count; j++)
            {
                Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();
                treeNode.Text = TmpTable.Rows[j]["user_name"].ToString();
                treeNode.NodeID = TmpTable.Rows[j]["user_id"].ToString();
                treeNode.Leaf = true;
                treeNode.IconCls = "no-Icon";
                if (ischeck)
                    treeNode.Checked = ThreeStateBool.True;
                else
                    treeNode.Checked = ThreeStateBool.False;
                nodes.Add(treeNode);
            }
        }
        return nodes.ToJson();
    }
    #endregion

    private void CreatTmpTable()
    {
        DataTable tmptable = new DataTable();
        if (Session["tmpUserTable"] == null)
        {
            tmptable.TableName = "tmpUserTable";
            tmptable.Columns.Add("USER_ID", Type.GetType("System.String"));
            tmptable.Columns.Add("USER_NAME", Type.GetType("System.String"));
            Session["tmpUserTable"] = tmptable;
        }
        else
        {
            tmptable = Session["tmpUserTable"] as DataTable;
        }
        BindGrid(tmptable);
    }
    //加载列表
    private void BindGrid(DataTable dt)
    {
        this.Store1.DataSource = dt;
        this.Store1.DataBind();
    }

    [DirectMethod]
    public void refrashDataEvent(string NodeIds)
    {
        string[] menuno = NodeIds.Split(';');
        for (int i = 0; i < menuno.Length; i++)
        {
            if (menuno[i].Substring(0, 1) == "A")
            {
                string deptId = menuno[i].Substring(1, menuno[i].Length - 1).Trim();
                GetUserDataTbByDeptId(deptId);
            }
            else
            {
                DataTable dtUser = CommonLib.BLL.SYS_USER.GetList(" user_id='" + menuno[i] + "'").Tables[0];
                if (dtUser.Rows.Count > 0)
                {
                    DataTable tmptable = Session["tmpUserTable"] as DataTable;
                    DataRow[] drArr = tmptable.Select(" USER_ID='" + dtUser.Rows[0]["user_id"].ToString() + "'");
                    if (drArr.Length < 1)
                    {
                        DataRow dr = tmptable.NewRow();
                        dr["USER_ID"] = dtUser.Rows[0]["user_id"].ToString();
                        dr["USER_NAME"] = dtUser.Rows[0]["user_name"].ToString();
                        tmptable.Rows.Add(dr);
                        Session["tmpUserTable"] = tmptable;
                    }
                }
            }

        }
        DataTable dtSource = Session["tmpUserTable"] as DataTable;
        Store1.DataSource = dtSource;
        Store1.DataBind();
    }

    //递归得到部门下人员的函数
    private void GetUserDataTbByDeptId(string deptId)
    {
        //得到选中部门下的人员
        DataTable dttUser = CommonLib.BLL.SYS_USER.GetList(" dept_id='" + deptId + "'").Tables[0];
        if (dttUser.Rows.Count > 0)
            GetUserDataTb(dttUser);
        //得到选中部门下的部门下的人员
        DataTable dttDept = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and rtrim(dept_id) like '" + deptId + "__' ", "dept_id").Tables[0];
        if (dttDept.Rows.Count > 0)
        {
            for (int j = 0; j < dttDept.Rows.Count; j++)
            {
                GetUserDataTbByDeptId(dttDept.Rows[j]["dept_id"].ToString().Trim());
            }
        }
    }
    //得到选中部门下的人员的函数


    private void GetUserDataTb(DataTable dtmp)
    {
        DataTable tmptable = Session["tmpUserTable"] as DataTable;
        for (int i = 0; i < dtmp.Rows.Count; i++)
        {
            DataRow[] drArr = tmptable.Select(" USER_ID='" + dtmp.Rows[i]["user_id"].ToString() + "'");
            if (drArr.Length < 1)
            {
                DataRow dr = tmptable.NewRow();
                dr["USER_ID"] = dtmp.Rows[i]["user_id"].ToString();
                dr["USER_NAME"] = dtmp.Rows[i]["user_name"].ToString();
                tmptable.Rows.Add(dr);
            }
        }
        Session["tmpUserTable"] = tmptable;
    }

    //获得选取用户的名字和ID字符串


    [DirectMethod]
    public string GetUserNameIds()
    {
        string result = "";
        DataTable dtGetUser = Session["tmpUserTable"] as DataTable;
        if (dtGetUser.Rows.Count > 0)
        {
            for (int i = 0; i < dtGetUser.Rows.Count; i++)
            {
                result += dtGetUser.Rows[i]["USER_NAME"].ToString()+",";
            }
            result = result.TrimEnd(',') + ";";
            for (int i = 0; i < dtGetUser.Rows.Count; i++)
            {
                result += dtGetUser.Rows[i]["USER_ID"].ToString() + ",";
            }
            result = result.TrimEnd(',');
            result = result + ";" + txtName+","+txtId;
        }
        return result;
    }
    protected void DelUser_Click(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }
        DataTable tmptable = Session["tmpUserTable"] as DataTable;
        foreach (SelectedRow row in sm.SelectedRows)
        {
            DataRow[] tR = tmptable.Select("USER_ID='" + row.RecordID + "'");
            tmptable.Rows.Remove(tR[0]);
        }
        tmptable.AcceptChanges();
        Session["tmpUserTable"] = tmptable;
        BindGrid(tmptable);
    }
}
